import {createItem} from '~/server/database/repositories/itemOperation'
import {getUserFromJwt} from '~/utils/serveCommon'
export default defineEventHandler( async(context) => {
  try{
    const paramData = await readBody(context) || {}
    const { happen_at, amount, tag_id, kind} = paramData
    if(!happen_at || !amount || !tag_id || !kind){
      return sendError(context,createError({
        statusCode: 400,
        statusMessage: '参数错误!',
      }))
    }
    const user_id = getUserFromJwt(context)
    const item = await createItem({...paramData, user_id})
    if(item){
      return {resource:item}
    }else{
      return sendError(context,createError({
        statusCode: 400,
        statusMessage: '创建账目失败!',
      }))
    }
  }
  catch(error){
    return sendError(context, createError({
      statusCode: 400,
      statusMessage: '新建账目失败!',
    }))
  }
})